/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taglibs.admin;

import daos.CategoryDAO;
import daos.ItemDAO;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import models.Category;
import models.Item;
import util.Common;
import util.WebConst;

/**
 *
 * @author trietto
 */
public class ItemFormEdit extends TagSupport {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /*
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
     */
    public int doStartTag() {
        try {
            JspWriter writer = pageContext.getOut();
            String actionName = pageContext.getRequest().getParameter(WebConst.ACTION);
            ArrayList<String> lstCategoriesData = new ArrayList<String>();
            ArrayList<Category> lstCategories = CategoryDAO.singleton().selectAll();            
            for (Category category : lstCategories) {
                lstCategoriesData.add(category.getName() + "\t" + category.getCategoryId());
            }

            if (actionName.equalsIgnoreCase(WebConst.EDIT)) {
                int itemId = Integer.parseInt(pageContext.getRequest().getParameter("itemid"));
                Item item = ItemDAO.singleton().select(itemId);
                if (item == null && actionName.equalsIgnoreCase(WebConst.EDIT)) {
                    return SKIP_BODY;
                }
                boolean isRecommend = item.getIsRecommended() == 1 ? true : false;
                writer.println("<form action='AdminItemServlet?action=update&itemid=" + itemId + "' method='POST'>");
                writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                writer.println("<tr>");
                writer.println("<td>Category:</td>");
                writer.println("<td>" + Common.createComboBox("cbCategory", "cbCategory", (String[])(lstCategoriesData.toArray(new String[0])), String.valueOf(item.getCategoryId()), "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Author:</td>");
                writer.println("<td>" + Common.createTextbox("txtAuthor", "txtAuthor", item.getAuthor(), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Name:</td>");
                writer.println("<td>" + Common.createTextbox("txtName", "txtName", item.getName(), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>ImageURL:</td>");
                writer.println("<td>" + Common.createTextbox("txtImageURL", "txtImageURL", item.getImageURL(), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>ProductURL:</td>");
                writer.println("<td>" + Common.createTextbox("txtProductURL", "txtProductURL", item.getProductURL(), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Notes:</td>");
                writer.println("<td>" + Common.createTextbox("txtNotes", "txtNotes", item.getNotes(), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Price:</td>");
                writer.println("<td>" + Common.createTextbox("txtPrice", "txtPrice", String.valueOf(item.getPrice()), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Recommend:</td>");
                writer.println("<td>" + Common.createCheckBox("chkRecommend", "chkRecommend", isRecommend, "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr colspan='2'>");
                writer.println("<td>" + Common.createSubmitButton("btnUpdate", "btnUpdate", "Update", "") + "</td>");
                writer.println("</tr>");
                writer.println("</table>");
                writer.println("</form>");
            } else if (actionName.equalsIgnoreCase(WebConst.NEW)) {
                writer.println("<form action='AdminItemServlet?action=create' method='POST'>");
                writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                writer.println("<tr>");
                writer.println("<td>Category:</td>");
                writer.println("<td>" + Common.createComboBox("cbCategory", "cbCategory", (String[])(lstCategoriesData.toArray(new String[0])), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Author:</td>");
                writer.println("<td>" + Common.createTextbox("txtAuthor", "txtAuthor", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Name:</td>");
                writer.println("<td>" + Common.createTextbox("txtName", "txtName", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>ImageURL:</td>");
                writer.println("<td>" + Common.createTextbox("txtImageURL", "txtImageURL", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>ProductURL:</td>");
                writer.println("<td>" + Common.createTextbox("txtProductURL", "txtProductURL", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Notes:</td>");
                writer.println("<td>" + Common.createTextbox("txtNotes", "txtNotes", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Price:</td>");
                writer.println("<td>" + Common.createTextbox("txtPrice", "txtPrice", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Recommend:</td>");
                writer.println("<td>" + Common.createCheckBox("chkRecommend", "chkRecommend", false, "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr colspan='2'>");
                writer.println("<td>" + Common.createSubmitButton("btnCreate", "btnCreate", "Create", "") + "</td>");
                writer.println("</tr>");
                writer.println("</table>");
                writer.println("</form>");
            }

        } catch (Exception e) {
            Logger.getLogger(ItemFormEdit.class.getName()).log(Level.SEVERE, null, e);
        }
        return SKIP_BODY;
    }
}
